package com.kaifamiao.context;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        final ServletContext application = this.getServletContext();
        String ctxPath = application.getContextPath();

        /*
        // String path = ctxPath + "/counter"; // CounterServlet
        // String path = ctxPath + "/init/param"; // InitParamServlet
        String path = ctxPath + "/counter.jsp"; // counter.jsp
        // 根据即将访问的目标资源路径获得请求调度器
        // RequestDispatcher dispatcher = request.getRequestDispatcher( path );
        RequestDispatcher dispatcher = application.getRequestDispatcher( path );
        dispatcher.forward(request, response);
        */

        String servletName = "init-param-servlet";
        // 根据即将访问的servlet名称获得请求调度器
        RequestDispatcher dispatcher = application.getNamedDispatcher( servletName );
        dispatcher.forward(request, response);
    }
}
